

# ME218 Arduino makefile
# 21 Mar 11



# Update these to reflect the location of the Arduino IDE and avrdude installations
# ARDUINO = \
# /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino

ARDUINO = /Users/rick/Documents/Arduino/libraries/Cores/arduino
AVRDUDE_DIR = /usr/local/CrossPack-AVR/bin

C_MODULES = \
#$(CURDIR)/ME218_I2C_top.c\
$(CURDIR)/ME218_I2C.c \
$(CURDIR)/ME218_CLUT_PGM.c \
$(CURDIR)/ME218_CLUT.c \
$(CURDIR)/ME218_Sprite_PGM.c \
$(CURDIR)/ME218_Sprites.c \
$(CURDIR)/ME218_ISR.c \
$(CURDIR)/ME218_work.c 
# end of C_MODULES


CXX_MODULES = \
$(CURDIR)/ME218_I2C_top.c\
$(CURDIR)/ME218_I2C.c \
$(CURDIR)/ME218_CLUT_PGM.c \
$(CURDIR)/ME218_CLUT.c \
$(CURDIR)/ME218_Sprite_PGM.c \
$(CURDIR)/ME218_Sprites.c \
$(CURDIR)/ME218_ISR.c \
$(CURDIR)/ME218_work.c \
$(CURDIR)/main.cpp 
# end of CXX_MODULES

#PORT = usb
PORT = /dev/tty.usbserial*
TARGET = main
#SRC		= $(C_MODULES)
#CXXSRC	= $(CXX_MODULES)
 
MCU = atmega328p
F_CPU = 16000000
FORMAT = ihex
UPLOAD_RATE = 57600

# Name of this Makefile (used for "make depend").
MAKEFILE = Makefile

# Debugging format.
# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
# AVR (extended) COFF requires stabs, plus an avr-objcopy run.
DEBUG = stabs

#s optimize for size
#0 optimization off
OPT = s

# Place -D or -U options here
CDEFS = -DF_CPU=$(F_CPU)
CXXDEFS = -DF_CPU=$(F_CPU)

# Place -I options here
CINCS = -I$(ARDUINO)
#CINCS =

CXXINCS = -I$(ARDUINO)
#CXXINCS = 


# Compiler flag to set the C Standard level.
# c89   - "ANSI" C
# gnu89 - c89 plus GCC extensions
# c99   - ISO C99 standard (not yet fully implemented)
# gnu99 - c99 plus GCC extensions
CSTANDARD = -std=gnu99
CDEBUG = -g$(DEBUG)
CWARN = -Wall -Wstrict-prototypes
CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums \
	-ffunction-sections -fdata-sections -Wl,-gc-sections
#CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums \
#	-ffunction-sections -fdata-sections 


CFLAGS =   $(CDEFS) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) $(CDEBUG) 
CXXFLAGS = $(CXXDEFS) -O$(OPT) $(CDEBUG)
LDFLAGS = -lm -Wl,-gc-sections
#LDFLAGS = -lm -Wl,-gc-sections -KEEP(*(.data)) 
#LDFLAGS = -lm -KEEP(*(.data))
#-Wl,-gc-sections -KEEP(*(.data)) 

ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp 

# Programming support using avrdude. Settings and variables.
AVRDUDE_PROGRAMMER = stk500v1
AVRDUDE_PORT = $(PORT)
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
AVRDUDE_FLAGS = -F -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) \
	-b $(UPLOAD_RATE) -D \
#		-q
	
# We use single quiet so app size is displayed
# -q -q indicates "double quiet"
# -u allows fuses to be changed
#AVRDUDE_FUSE = -e -u -U lfuse:w:0x62:m -U hfuse:w:0xd9:m -U efuse:w:0xff:m 
AVRDUDE_FUSE = 

 
# Program settings
CC = $(AVRDUDE_DIR)/avr-gcc
CXX = $(AVRDUDE_DIR)/avr-g++
OBJCOPY = $(AVRDUDE_DIR)/avr-objcopy
OBJDUMP = $(AVRDUDE_DIR)/avr-objdump
AR  = $(AVRDUDE_DIR)/avr-ar
SIZE = $(AVRDUDE_DIR)/avr-size
NM = $(AVRDUDE_DIR)/avr-nm
AVRDUDE = $(AVRDUDE_DIR)/avrdude
REMOVE = rm -f
MV = mv -f

# Define all object files.
#@OBJ = $(CURDIR)/ME218_I2C_top.o
#OBJ = $(SRC:.c=.o) $(CXXSRC:.cpp=.o) $(ASRC:.S=.o
OBJ = \
$(CURDIR)/ME218_I2C_top.o\
$(CURDIR)/ME218_I2C.o \
$(CURDIR)/ME218_CLUT_PGM.o \
$(CURDIR)/ME218_CLUT.o \
$(CURDIR)/ME218_Sprite_PGM.o \
$(CURDIR)/ME218_Sprites.o \
$(CURDIR)/ME218_ISR.o \
$(CURDIR)/ME218_work.o \
$(CURDIR)/main.o 
# end of CXX_MODULES



# Define all listing files.
#@LST = $(ASRC:.S=.lst) $(CXXSRC:.cpp=.lst) $(SRC:.c=.lst)

# Combine all necessary flags and optional flags.
# Add target processor to flags.
#ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)
#ALL_CXXFLAGS = -mmcu=$(MCU) -I. $(CXXFLAGS)
# ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)


# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
COFFCONVERT=$(OBJCOPY) --debugging \
--change-section-address .data-0x800000 \
--change-section-address .bss-0x800000 \
--change-section-address .noinit-0x800000 \
--change-section-address .eeprom-0x810000 

# ===================== where-am-i =========================


where-am-i = $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
#	$(warning $(THIS_MAKEFILE))

THIS_MAKEFILE := $(call where-am-i)
#PATH := $(PATH)

# ====================== Targets =========================

#$(ARDUINO)/WInterrupts.o: $(ARDUINO)/WInterrupts.c
#	$(CC) -c $(ARDUINO)/WInterrupts.c -mmcu=$(MCU) -I. $(CFLAGS) $(CINCS) $(CTUNING) \
#	-o $(ARDUINO)/WInterrupts.o
	
#$(ARDUINO)/HardwareSerial.o: $(ARDUINO)/HardwareSerial.cpp
#	$(CPP) -c $(ARDUINO)/HardwareSerial.cpp -mmcu=$(MCU) -I. $(CXXFLAGS) $(CINCS) $(CTUNING) \
#	-o $(ARDUINO)/HardwareSerial.o

#$(ARDUINO)/Print.o: $(ARDUINO)/Print.cpp
#	$(CPP) -c $(ARDUINO)/Print.cpp -mmcu=$(MCU) -I. $(CXXFLAGS) $(CINCS) $(CTUNING) \
#	-o $(ARDUINO)/Print.o

	
$(CURDIR)/ME218_I2C_top.o: $(CURDIR)/ME218_I2C_top.c $(CURDIR)/ME218_I2C.h
	@$(CPP) -c $(CURDIR)/ME218_I2C_top.c -mmcu=$(MCU) -I. $(CXXFLAGS) $(CINCS) $(CTUNING) \
	-o $(CURDIR)/ME218_I2C_top.o 

	
$(CURDIR)/ME218_I2C.o: $(CURDIR)/ME218_I2C.c $(CURDIR)/ME218_I2C.h
	@$(CPP) -c $(CURDIR)/ME218_I2C.c -mmcu=$(MCU) -I. $(CXXFLAGS) $(CINCS) $(CTUNING) \
	-o $(CURDIR)/ME218_I2C.o

$(CURDIR)/ME218_CLUT_PGM.o: $(CURDIR)/ME218_CLUT_PGM.c $(CURDIR)/ME218_CLUT.h
	@$(CPP) -c $(CURDIR)/ME218_CLUT_PGM.c -mmcu=$(MCU) -I. $(CXXFLAGS) $(CINCS) $(CTUNING) \
	-o $(CURDIR)/ME218_CLUT_PGM.o
	
$(CURDIR)/ME218_CLUT.o: $(CURDIR)/ME218_CLUT.c $(CURDIR)/ME218_CLUT.h
	@$(CPP) -c $(CURDIR)/ME218_CLUT.c -mmcu=$(MCU) -I. $(CXXFLAGS) $(CINCS) $(CTUNING) \
	-o $(CURDIR)/ME218_CLUT.o

$(CURDIR)/ME218_Sprite_PGM.o: $(CURDIR)/ME218_Sprite_PGM.c $(CURDIR)/ME218_Sprites.h
	@$(CPP) -c $(CURDIR)/ME218_Sprite_PGM.c -mmcu=$(MCU) -I. $(CXXFLAGS) $(CINCS) $(CTUNING) \
	-o $(CURDIR)/ME218_Sprite_PGM.o
	
$(CURDIR)/ME218_Sprites.o: $(CURDIR)/ME218_Sprites.c $(CURDIR)/ME218_Sprites.h
	@$(CPP) -c $(CURDIR)/ME218_Sprites.c -mmcu=$(MCU) -I. $(CXXFLAGS) $(CINCS) $(CTUNING) \
	-o $(CURDIR)/ME218_Sprites.o

$(CURDIR)/ME218_ISR.o: $(CURDIR)/ME218_ISR.c $(CURDIR)/ME218_ISR.h 
	@$(CPP) -c $(CURDIR)/ME218_ISR.c -mmcu=$(MCU) -I. $(CXXFLAGS) $(CINCS) $(CTUNING) \
	-o $(CURDIR)/ME218_ISR.o

$(CURDIR)/ME218_work.o: $(CURDIR)/ME218_work.c $(CURDIR)/ME218_work.h
	@$(CPP) -c $(CURDIR)/ME218_work.c -mmcu=$(MCU) -I. $(CXXFLAGS) $(CINCS) $(CTUNING) \
	-o $(CURDIR)/ME218_work.o

$(CURDIR)/main.o: $(CURDIR)/main.cpp
	@$(CPP) -c $(CURDIR)/main.cpp -mmcu=$(MCU) -I. $(CXXFLAGS) $(CINCS) $(CTUNING) \
	-o $(CURDIR)/main.o

# ============ all =================
all: build
	@echo "<<<<< ME218 all: >>>>>"
	@echo " "

# ============ build =================

build: elf hex
	@echo "<<<<< ME218 build:>>>>> "
	@echo " "
	
# ============ RICK =================

RICK:
	@echo "<<<<<  ME218 Ready... >>>>> "
#	cp $(CURDIR)/ME218_ISR.s $(CURDIR)/ME218_ISR.s

	

#	screen -d -m $(AVRDUDE_PORT)
	@echo " "


# ============ elf =================

elf: $(TARGET).elf
	@echo "<<<<< ME218 elf: >>>>>"
	@echo " "

# ============ hex =================
hex: $(TARGET).hex
	@echo "<<<<< ME218 hex: >>>>>"
	@echo " "

# ============ eep =================
eep: $(TARGET).eep
	@echo "<<<<< ME218 eep: >>>>>"
	@echo " "
	
# ============ lss =================
lss: $(TARGET).lss 
	@echo "<<<<< ME218 lss: >>>>>"
	@echo " "

# ============ sym =================
sym: $(TARGET).sym
	@echo "<<<<< ME218 sym: >>>>>"
	@echo " "


# =============== Upload: Program the device ===============  
upload: $(TARGET).hex $(TARGET).lss
	@echo "<<<<< ME218 upload: >>>>>"
	@$(AVRDUDE) $(AVRDUDE_FUSE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) 
#	@echo "<<<<< end of upload >>>>>"
#	@echo " "


# =========== coff ==============

coff: $(TARGET).elf
	@echo "<<<<< ME218 coff: >>>>>"
	$(COFFCONVERT) -O coff-avr $(TARGET).elf $(TARGET).cof
	@echo " "


# =========== extcoff ==============

extcoff: $(TARGET).elf
	@echo "<<<<< ME218 extcoff: >>>>>"
	$(COFFCONVERT) -O coff-ext-avr $(TARGET).elf $(TARGET).cof
	@echo
	

# =========== .SUFFIXES ==============

.SUFFIXES: .elf .hex .eep .lss .sym
	@echo "<<<<< ME218 .SUFFIXES: >>>>>"
	@echo " "

# =========== .elf.hex ==============

.elf.hex: $(TARGET).elf
	@echo "<<<<< ME218 .elf.hex: >>>>>"
	@$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
#	@echo "<<<<< end of elf.hex >>>>>"
#	@echo " "

# =========== .elf.eep ==============

.elf.eep:
	@echo "<<<<< ME218 >>>>> .elf.eep:"
	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
	--change-section-lma .eeprom=0 -O $(FORMAT) $< $@
	@echo " "


# =========== Create extended listing file from ELF output file ==============
.elf.lss: $(TARGET).elf
	@echo "<<<<< ME218 .elf.lss: >>>>>"
	@$(OBJDUMP)   -h --headers \
	-S --show-raw-insn --source \
	-t --syms \
	-l --line-numbers \
	-C --demangle \
	-g --debugging \
	-d --disassemble \
	-z --disassemble-zeroes \
	$< > $(CURDIR)/$(TARGET).lss
#	@echo " "

#	-s --full-contents \
#	-z --disassemble-zeroes \

# --no-show-raw-insn
# =============== Create a symbol table from ELF output file ============
.elf.sym:
	@echo "<<<<< ME218 .elf.sym: >>>>>"
	$(NM) -n $< > $@
	@echo "<<<<< end of elf.sym >>>>>"
	@echo " "

# ================== core.a ===========================

core.a: $(OBJ)
	@echo "<<<<< ME218 core.a: >>>>>"
#	@for i in $(OBJ); do echo $(AR) rcs core.a $$i; $(AR) rcs core.a $$i; done
	@for i in $(OBJ); do $(AR) rcs core.a $$i; done

#	@for i in $(OBJ); do $(AR) rcs core.a $$i; done
#	@echo "<<<<< end of core.a >>>>>"
#	@echo " "


# ============== Link: create ELF output file from library ==================

$(TARGET).elf: core.a
#	@echo "<<<<< ME218 >>>>>"
#	@echo "<<<<< ME218 .core.elf  TARGET: " $(TARGET) ".elf  >>>>>"
	@echo "<<<<< ME218 .core.elf: >>>>> !!! -L. core.a is not working, not essential!!!"

#	@echo $(CXX)
#	@echo -mmcu=$(MCU) -I.
#	@echo $(CXXFLAGS)
#	@echo $(CINCS)
#	@echo $(CTUNING)
#	@echo $(LDFLAGS)
#	@echo $(TARGET).cpp -L. core.a -o $@ 
	
# !!!!!!!!!!!!! core.a is not working !!!!!!!!!!!!!
	@$(CXX) -mmcu=$(MCU) -I. $(CXXFLAGS) $(CINCS) $(CTUNING) $(LDFLAGS)  \
	$(TARGET).cpp -o $@
#	$(TARGET).cpp -L. core.a -o $@ 

#	-L. core.a -o main.elf


#	@$(CXX) -mmcu=$(MCU) -I. $(CXXFLAGS) $(CINCS) $(CTUNING) $(LDFLAGS)  \
	$(TARGET).cpp -o $@
#	$(TARGET).cpp -L. core.a -o $@

#	$(TARGET).cpp -o $@
#	$(TARGET).cpp -L. core.a -o $@
#	$(TARGET).cpp -o $@
	
#	@echo "<<<<< end of TARGET:elf >>>>>"		# runs after all the warnings
#	@echo " "


# =========== Compile: create object files from C++ source files ==============

#.cpp.o:
#	@echo "<<<<< ME218 .ccp.o: >>>>>"
	
#	@echo $(CXX)
#	@echo -c -mmcu=$(MCU) -I.
#	@echo $(CXXFLAGS)
#	@echo $(CINCS)
#	@echo $(CTUNING)

#	@for i in $@; do echo $<; done
#	$(CXX) -c -mmcu=$(MCU) -I. $(CXXFLAGS) $(CINCS) $(CTUNING) $< -o $@ 
#	@echo "<<<<< end of cpp.o. >>>>>"
#	@echo " "


# ========== Compile: create object files from C source files ================
#.c.o:
#	@echo "<<<<< ME218 .c.o: >>>>> "
	
#	@echo $(CC)
#	@echo -c -mmcu=$(MCU) -I.
#	@echo $(CFLAGS)
#	@echo $(CINCS)
#	@echo $(CTUNING)

#	@for i in $<@; do echo $<; done
#	@$(CC) -c -mmcu=$(MCU) -I. $(CFLAGS) $(CINCS) $(CTUNING) $< -o $@ 
#	@echo "<<<<< end of c.o. >>>>>"
#	@echo " "


# ========== Compile: create assembler files from C source files ============

.c.s:
	@echo "<<<<< ME218 .c.s: >>>>>"
		
	@echo -S -mmcu=$(MCU) -I.
	@echo $(CFLAGS)
	@echo $(CINCS)
	@echo $(CTUNING)

#	@for i in $<@; do echo $<; done
	@$(CC) -S -mmcu=$(MCU) -I. $(CFLAGS) $(CINCS) $(CTUNING) $< -o $@ 
#	@echo "<<<<< end of c.s. >>>>>"
#	@echo " "


# ====== Assemble: create object files from assembler source files =========
.S.o:
	@echo "<<<<< ME218 .S.o: >>>>>"
	@echo $(CC)
	@echo -c $(ASFLAGS)

#	@for i in $@; do echo $<; done
#	@for i in $@; do echo $<; done
	@$(CC) -c $(ASFLAGS) $< -o $@ 
#	@echo "<<<<< end of S.o. >>>>>"
#	@echo " "


# ================= clean project ========================
clean:

#	@echo "<<<<< gcc compiler version: >>>>> "	
#	@$(CC) -v


	@echo "<<<<< ME218 clean: >>>>> "	
	@echo "<<<<< ME218 Makefile PATH: " $(where-am-i) " >>>>>"
	@echo "<<<<< ME218 Arduino  PATH: " $(ARDUINO) " >>>>>"
	@echo "<<<<< ME218 avr-gcc  PATH: " $(AVRDUDE_DIR) " >>>>>"
#	@echo " "
	

	@for i in $(OBJ)			; do $(REMOVE) $$i; done
#	@for i in $(OBJ)			; do $(REMOVE) $$i; echo "junk" $$i; done
	@for i in $(LST)			; do $(REMOVE) $$i; done
	@for i in $(SRC:.c=.s)		; do $(REMOVE) $$i; done
	@for i in $(SRC:.c=.d)		; do $(REMOVE) $$i; done
	@for i in $(CXXSRC:.cpp=.s)	; do $(REMOVE) $$i; done
	@for i in $(CXXSRC:.cpp=.d)	; do $(REMOVE) $$i; done


	@$(REMOVE) $(TARGET).hex 
	@$(REMOVE) $(TARGET).eep 
	@$(REMOVE) $(TARGET).cof 
	@$(REMOVE) $(TARGET).elf 
	@$(REMOVE) $(TARGET).map 
	@$(REMOVE) $(TARGET).sym 
	@$(REMOVE) $(TARGET).lss 
	@$(REMOVE) core.a
#	@echo "<<<<< end of clean: >>>>>"
#	@echo " "

# ================= depend ========================

depend:
	@echo "<<<<< ME218 depend: >>>>>"
	if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \
	then \
		sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \
			$(MAKEFILE).$$$$ && \
		$(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \
	if
	echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \
		>> $(MAKEFILE); \
	$(CC) -M -mmcu=$(MCU) $(CDEFS) $(CINCS) $(SRC) $(ASRC) >> $(MAKEFILE)
	@echo " "


# ================= PHONY ========================

#.PHONY:	all build elf hex eep lss sym program coff extcoff clean depend
# why is clean inside of PHONY? what is PHONY?
.PHONY:	all build elf hex eep lss sym program coff extcoff depend clean
	@echo "<<<<< ME218 PHONY: >>>>>"
	@echo " "

